// course/pages/type/index.js
const $api = require("../../../api/course").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    courseList:[],
    page:1,
    tablist:[],
    tabindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    t.setData({
      typeid:options.type
    })
    t.getCourseSearchOrder()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var t = this
    t.setData({
      page:t.data.page +1 
    })
  },
  // 获取在线课程专辑排序类型
  getCourseSearchOrder(){
    var t = this,
    data = {
      dict_type:"CourseSearchOrder",
    }
    $api.getCourseSearchType(data).then(res=>{
      console.log(res)
      t.setData({
        tablist:res.data,
        tabindex:0,
      })
      t.getOnlineCourseListByCourseType()
    })
  },
  gotoinfo(e){
    var t = this
    wx.navigateTo({
      url:'/course/pages/info/index'
    })
  },
  changetab(e){
    var t = this
    console.log(e)
    t.setData({
      tabindex:e.currentTarget.dataset.index,
      page:1,
      courseList:[]
    })
    t.getOnlineCourseListByCourseType()

  },
  // 根据类型获取在线课程列表
  getOnlineCourseListByCourseType(){
    var t = this,
    data = {
      member_id:wx.getStorageSync('memberinfo').member_id,
      member_type:wx.getStorageSync('memberinfo').member_type,
      search_order:t.data.tablist[t.data.tabindex].dict_key,
      course_type:t.data.typeid,
      search_order_rule:"ASC",
      pageNo:t.data.page,
    }
    $api.getOnlineCourseListByCourseType(data).then(res=>{
      var courseList = t.data.courseList
      courseList = courseList.concat(res.data)
      t.setData({
        courseList:courseList
      })
    })
  },
  gotoinfo(e){
    var t = this
    wx.navigateTo({
      url:'/course/pages/info/index?id='+e.currentTarget.dataset.id
    })
  },
})